/**
 * @author Limohan
 */
package dao;

import com.alibaba.fastjson.JSON;
import pojo.product.ProductMsgEntity;
import utils.DataFileName;
import utils.DataUtils;
import utils.JsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ProductMsgDao {
    public void saveMsg(ProductMsgEntity msg) {
        String json = JSON.toJSONString(msg);
        try {
            DataUtils.writeData(DataFileName.PRODUCTMSG.toString(), json);
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
    }

    public List<ProductMsgEntity> findMsgList() throws IOException {
        String json = DataUtils.readData(DataFileName.PRODUCTMSG.getName());
        if (json.length() == 0) {
            return null;
        }
        List<ProductMsgEntity> msgEntities = new ArrayList<>();
        String[] types = json.split("/");
        for (String str : types) {
            ProductMsgEntity msgEntity = JsonUtil.jsonToObject(str, ProductMsgEntity.class);
            msgEntities.add(msgEntity);
        }
        return msgEntities;
    }

    public List<ProductMsgEntity> findMsgByName(String name) throws IOException {
        List<ProductMsgEntity> productMsgEntities = findMsgList();
        List<ProductMsgEntity> productMsgEntities1 = new ArrayList<>();
        if (productMsgEntities != null) {
            for (ProductMsgEntity productMsgEntity : productMsgEntities) {
                if (name.equals(productMsgEntity.getName())) {
                    productMsgEntities1.add(productMsgEntity);
                }
            }
            return productMsgEntities1;
        }
        return null;

    }

    public void changeType(String oldType, String newType) throws IOException {
        List<ProductMsgEntity> productMsgEntities = findMsgList();
        if (productMsgEntities != null) {
            for (ProductMsgEntity productMsgEntity : productMsgEntities) {
                if (productMsgEntity.getType().equals(oldType))
                    productMsgEntity.setType(newType);
            }
            DataUtils.clearData(DataFileName.PRODUCTMSG.toString());
            for (ProductMsgEntity msgEntity : productMsgEntities) {
                this.saveMsg(msgEntity);
            }
        }

    }

    public void deletType(String type) throws Exception {
        List<ProductMsgEntity> productMsgEntities = findMsgList();
        if (productMsgEntities != null) {
            for (ProductMsgEntity msgEntity : productMsgEntities) {
                if (msgEntity.getType().equals(type))
                    msgEntity.setType("");
            }
            DataUtils.clearData(DataFileName.PRODUCTMSG.toString());
            for (ProductMsgEntity msgEntity : productMsgEntities) {
                this.saveMsg(msgEntity);
            }
        }
    }

    public String getMaxId() throws IOException {
        List<ProductMsgEntity> productMsgEntities = findMsgList();
        int max = 0;
        if (productMsgEntities == null || productMsgEntities.size() == 0) {
            return "1";
        } else {
            for (ProductMsgEntity productMsgEntity : productMsgEntities) {
                if (Integer.parseInt(productMsgEntity.getId()) > max) {
                    max = Integer.parseInt(productMsgEntity.getId());
                }
            }
            return String.valueOf(max + 1);
        }
    }

    public void deleteMsg(String id) throws IOException {
        List<ProductMsgEntity> productMsgEntities = findMsgList();
        if (productMsgEntities != null) {
            productMsgEntities.removeIf(productMsgEntity -> productMsgEntity.getId().equals(id));
            DataUtils.clearData(DataFileName.PRODUCTMSG.toString());
            for (ProductMsgEntity productMsgEntity : productMsgEntities) {
                this.saveMsg(productMsgEntity);
            }
        }

    }

    public boolean isProduct(String type) throws IOException {
        ProductTypeDao productTypeDao = new ProductTypeDao();
        return productTypeDao.isType(type);
    }

    public ProductMsgEntity findMsg(String id) throws IOException {
        List<ProductMsgEntity> productMsgEntityList = findMsgList();
        if (productMsgEntityList != null) {
            for (ProductMsgEntity productMsgEntity : productMsgEntityList) {
                if (id.equals(productMsgEntity.getId())) {
                    return productMsgEntity;
                }
            }
        }
        return null;
    }
}
